"""
用户视图层
"""
from interface import user_interface
from interface import bank_interface
from lib import common

# 当前登录用户
login_user = None


# 1. 注册功能
def register():
    while True:
        # 输入用户名和密码
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()
        re_password = input('请再次输入密码：').strip()

        # 核验密码
        if password == re_password:
            # 调用注册接口
            flag, msg = user_interface.register_interface(
                username,
                password
            )
            if flag:
                print(msg)
                break
            else:
                print(msg)
                continue
        print('两次密码输入不一致，请重新输入')


# 2. 登录功能
def login():
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()

        flag, msg = user_interface.login_interface(username, password)
        print(msg)
        if flag:
            global login_user
            login_user = username
            break
        else:
            continue


# 3. 查看余额
@common.login_auth
def check_balance():
    balance = bank_interface.check_balance_interface(login_user)
    print(f"您的余额为[{balance}]")


# 4. 提现功能
@common.login_auth
def withdraw():
    while True:
        input_money = input("请输入提现金额：").strip()
        if not input_money.isdigit():
            continue

        flag, msg = bank_interface.withdraw_interface(login_user, input_money)
        print(msg)
        if flag:
            break
        else:
            continue


# 5. 还款功能
@common.login_auth
def repay():
    while True:
        input_money = input('请输入还款金额：')
        if not input_money.isdigit():
            continue

        input_money = int(input_money)
        if input_money > 0:
            flag, msg = bank_interface.repay_interface(
                login_user,
                input_money
            )
            print(msg)
            break
        else:
            print('请输入金额>0')


# 6. 转账功能
@common.login_auth
def transfer():
    while True:
        to_user = input('请输入目标用户：').strip()
        input_money = input('请输入转账金额：').strip()

        if not input_money.isdigit():
            print('请输入正确的金额！')
            continue

        input_money = int(input_money)

        if input_money > 0:
            flag, msg = bank_interface.transfer_interface(
                login_user,
                to_user,
                input_money
            )
            print(msg)
            if flag:
                break
            else:
                continue

        else:
            print('请输入正确的金额！')


# 7. 查看流水
@common.login_auth
def check_flow():
    flows = bank_interface.check_flow_interface(login_user)
    if flows:
        for flow in flows:
            print(flow)
    else:
        print('流水为空')


# 8. 购物功能
@common.login_auth
def shopping():
    shop_list = [
        ['面包', 30],
        ['鸡爪', 10],
        ['蛋糕', 50],
        ['土豆', 20],
    ]

    # 打印商品信息
    while True:
        # 枚举enumerate(可迭代对象) -> (可迭代对象的索引，索引对应值)
        for index, shop in enumerate(shop_list):
            shop_name, shop_price = shop
            print(
                f'商品编号：{index}',
                f'商品名称：{shop_name}',
                f'商品价格：{shop_price}',
            )

        choice = input('请输入商品编号:').strip()

        if not choice.isdigit():
            print('请输入正确的编号')
            continue
        choice=int(choice)
        # 判断choice是否存在
        if choice not in range(len(shop_list)):
            print("请输入正确编号")
            continue

        # 获取商品名称和单价
        shop_name, shop_price = shop_list[choice]


# 9. 查看购物车
@common.login_auth
def check_shop_car():
    pass


@common.login_auth
# 10. 管理员功能
def admin():
    from core import admin
    admin.admin_run()


# 函数功能字典
func_dic = {
    '1': register,
    '2': login,
    '3': check_balance,
    '4': withdraw,
    '5': repay,
    '6': transfer,
    '7': check_flow,
    '8': shopping,
    '9': check_shop_car,
    '10': admin,
}


# 视图层主程序
def run():
    while True:
        print('''
    =======  ATM + 购物车  =======
            1. 注册功能
            2. 登录功能
            3. 查看余额
            4. 提现功能
            5. 还款功能
            6. 转账功能
            7. 查看流水
            8. 购物功能
            9. 查看购物车
            10. 管理员功能
    ==========    END    ==========
        ''')

        choice = input('请输入功能编号：').strip()

        if choice not in func_dic:
            print('请输入正确的功能编号！')
            continue

        func_dic.get(choice)()
